<template>
	<page-meta :page-style="'overflow:'+(isShowGroupPopup||isDrawPop?'hidden':'visible')"></page-meta>
	<view class="mygroups" v-if="info">
		<view v-if="userinfo.club_join_perfect_status==1" class="mygroups_tips" @click="goGroupJoinCars">
			<view class="flex-1 display_flex">
				<view class="my_cell_dot"></view>
				<view class="f-12 ml-10">{{userinfo.club_join_perfect_text2}}</view>
			</view>
			<uni-icons type="closeempty" color="#999999" size="18"></uni-icons>
		</view>
		<view class="mygroups_box">
			<view class="groupinfo_box">
				<view class="groupinfo_top">
					<view class="group_img_box">
						<image class="group_img" :src="picUrl + info.logo" mode="aspectFill"></image>
						<image v-if="info.club_border" class="group_border_img" :src="picUrl + info.club_border"
							mode="aspectFill"></image>
					</view>
					<view class="group_title_box">
						<view class="display_flex" style="justify-content: space-between;">
							<view class="f-18 f-w" @click="goInfo">{{ info.title }}</view>
							<image @click="showGroupPopup" class="group_qrcode_mini" :src="onlinePic+'qrcode.png'">
							</image>
						</view>
						<view class="group_addr_box" v-if="info.address">
							<uni-icons type="location" size="12" color="#00A0DC"></uni-icons>
							<view>{{ info.address }}</view>
						</view>
						<view class="group_user_box">
							<view class="ellipsis flex-1" style="line-height: 200%;">创建者 | {{ info.manage_name }}</view>
							<view>{{ info.join_count }}人已申请加入</view>
						</view>
					</view>
				</view>
				<view class="groupinfo_text">
					<text>简介：{{ info.desc }}</text>
				</view>
				<view class="display_flex mt-20" style="align-items: flex-end;position: relative;">
					<view class="f-16 f-w">车友会成员</view>
					<view class="f-13 c-999999 ml-10">{{ info.number }}人</view>
					<view v-if="isDraw" class="draw_tips_boxd" @click="goDrawPage" style="right: -30rpx;">
						<image class="draw_tips_img" :src="picUrl+lotteryinfo.icon"></image>
					</view>
				</view>
				<view class="groups_user_box">
					<uni-row :gutter="15">
						<uni-col v-for="(item,index) in userlist" :key="item.user_id" :span="5" v-if="index<pageNums">
							<view class="text-c mt-20">
								<view class="mygroups_avatar_box">
									<image class="groups_user_img" :src="picUrl + item.avatar" mode="aspectFill">
									</image>
									<image v-if="item.avatar_border" class="mygroups_avatar_border"
										:src="picUrl+item.avatar_border"></image>
								</view>
								<view class="grouplist_name_box">
									<view class="c-999999 f-12 ellipsis">
										{{ item.username }}
									</view>
								</view>
							</view>
						</uni-col>
					</uni-row>
					<view @click="showMore()" class="text-c mt-15" v-if="pageNums==20&&total>20">
						<view class="f-15">展开更多</view>
						<view class="mt-5">
							<uni-icons type="down" color="#000000" size="20"></uni-icons>
						</view>
					</view>
					<view @click="showLess()" class="text-c mt-15" v-if="pageNums!=20">
						<view class="f-15">收起更多</view>
						<view class="mt-5">
							<uni-icons type="up" color="#000000" size="20"></uni-icons>
						</view>
					</view>
					<view class="group_defult_btn_block mt-30" @click="quitGroup">退出车友会</view>
					<!-- <view class="page_bottom_loading" :style="'padding-bottom:'+pdbottom+'rpx;'">{{loadingTitle}}</view> -->
				</view>
			</view>
			<view class="guide_mask_box" v-if="isShowGroupPopup" @click="colseGroupPopup"></view>
			<view class="group_qrcode_popup_box" v-if="isShowGroupPopup">
				<image class="group_qrcode_popup_bk" :src="picUrl + info.poster" :show-menu-by-longpress="true"
					mode="widthFix"></image>
			</view>
			<view class="draw_pop" v-if="isDrawPop">
				<image class="win_pop_img" :src="picUrl+lotteryinfo.popup" mode="widthFix"></image>
				<view class="draw_pop_text">
					<view class="f-20 f-w">{{lotteryinfo.popup_title}}</view>
					<view class="f-18 mt-5">
						<text>{{lotteryinfo.popup_desc}}</text>
					</view>
				</view>
				<view class="draw_pop_btn" @click="goDrawPage">立即抽奖</view>
				<view class="draw_pop_close_icon_box">
					<uni-icons class="draw_pop_close_icon" @click="closeDrawPopup" type="close" color="#ffffff"
						size="50"></uni-icons>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		getClubMyClub,
		getClubMyClubUserList,
		quitGroup,
		getGroupUserInfo,
		getLotteryInfo
	} from '@/api/motortime.js';
	let app = getApp()
	export default {
		data() {
			return {
				info: '',
				params: {
					page: 1,
					size: 99999
				},
				total: 0,
				pageNums: 20,
				picUrl: app.globalData.picUrl,
				onlinePic: app.globalData.onlinePic,
				userlist: [],
				loadingTitle: '',
				userinfo: '',
				isShowGroupPopup: false,
				isDrawPop: false,
				isDraw: false,
				lotteryinfo: ''
			};
		},
		onShow() {
			if (!uni.getStorageSync('access_token')) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			if (uni.getStorageSync('myload')) {
				this.getClubMyClub()
				this.getClubMyClubUserList()
				this.getLotteryInfo()
				uni.removeStorage({
					key: 'myload'
				})
			}
		},
		onLoad() {
			this.getClubMyClub()
			this.getClubMyClubUserList()
			this.getLotteryInfo()
			var params = {
				event_code: '我的车友会',
				path: 'pages/my/mygroups',
				event_id: '',
				title: '',
				source_page: '',
				value: '',
			}
			app.BurialPoint(params)
		},
		methods: {
			getLotteryInfo() {
				getLotteryInfo().then(res => {
					if (res.state == 1) {
						this.lotteryinfo = res.data.info
						if (res.data.info.status == 0) {
							this.isDrawPop = false
							this.isDraw = false
						} else if (res.data.info.status == 1) {
							this.isDrawPop = true
							this.isDraw = true
						} else if (res.data.info.status == 2) {
							this.isDraw = true
						}

					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			getClubMyClub() {
				getClubMyClub({}).then(res => {
					if (res.state == 1) {
						this.info = res.data.info
						getGroupUserInfo({
							user_id: uni.getStorageSync('userinfo') ? uni.getStorageSync('userinfo')
								.user_id : ''
						}).then(res => {
							if (res.state == 1) {
								this.userinfo = res.data.list
								uni.setStorage({
									key: 'userinfo',
									data: res.data.list,
									success: function() {}
								});
							} else {
								uni.removeStorage({
									key: 'access_token'
								})
							}
						})
					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 2000)
					}
				})
			},
			getClubMyClubUserList() {
				getClubMyClubUserList(this.params).then(res => {
					if (res.state == 1) {
						this.userlist = res.data.data
						this.total = res.data.total
					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			showMore() {
				this.pageNums = 99999
			},
			showLess() {
				this.pageNums = 20
			},
			showGroupPopup() {
				this.isShowGroupPopup = true
			},
			colseGroupPopup() {
				this.isShowGroupPopup = false
			},
			quitGroup() {
				var that = this
				var params = {
					event_code: '我的车友会',
					path: 'pages/my/mygroups',
					event_id: '',
					title: '',
					source_page: '退出车友会',
					value: '',
				}
				app.BurialPoint(params)
				// content: '确定退出' + that.info.title + '吗？',
				uni.showModal({
					content: '确定退出此车友会吗?',
					confirmColor: '#00A0DC',
					success: function(res) {
						if (res.confirm) {
							quitGroup({
								club_id: that.info.club_id,
							}).then(res => {
								if (res.state == 1) {
									uni.showToast({
										title: '已退出',
										mask: true,
										icon: 'success',
										duration: 1500
									})
									uni.setStorage({
										key: 'onload',
										data: 'true',
										success() {}
									})
									setTimeout(function() {
										uni.navigateBack({
											delta: 1
										})
									}, 1000)

								} else {
									uni.showToast({
										title: res.error[0],
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
							that.isSubmit = true
						}
					}
				});
			},
			goInfo() {
				var params = {
					event_code: '我的车友会',
					path: 'pages/my/mygroups',
					event_id: '',
					title: '',
					source_page: '点击进详情',
					value: '',
				}
				app.BurialPoint(params)
				uni.navigateTo({
					url: '/pages/group/groupinfo?club_id=' + this.info.club_id
				})
			},
			goGroupJoinCars() {
				uni.navigateTo({
					url: '/pages/group/groupjoincars'
				})
			},
			closeDrawPopup() {
				this.isDrawPop = false
			},
			goDrawPage() {
				if (this.lotteryinfo.status == 1) {
					uni.navigateTo({
						url: '/pages/draw/draw'
					})
				} else if (this.lotteryinfo.status == 2) {
					if (this.lotteryinfo.gifts_status == 0) {
						uni.navigateTo({
							url: '/pages/draw/draw'
						})
					} else {
						uni.navigateTo({
							url: '/pages/draw/drawresult'
						})
					}
				}
				var params = {
					event_code: '我的车友会',
					path: 'pages/my/mygroups',
					event_id: '',
					title: '',
					source_page: '立即抽奖',
					value: '',
				}
				app.BurialPoint(params)
			},
		},
		onPullDownRefresh() {
			this.getClubMyClub()
			this.getClubMyClubUserList()
			uni.stopPullDownRefresh()
		},
	}
</script>

<style lang="scss">
	.mygroups {

		.mygroups_tips {
			position: sticky;
			top: 0;
			left: 0;
			right: 0;
			display: flex;
			align-items: center;
			padding: 16rpx 30rpx;
			background-color: #FFE2E2;

			.my_cell_dot {
				width: 12rpx;
				height: 12rpx;
				margin-left: 15rpx;
				background-color: #F84949;
				border-radius: 50rpx;
			}
		}

		.mygroups_box {
			padding: 30rpx;

			.groupinfo_box {
				border-radius: 8rpx;
				padding: 50rpx 30rpx;
				background: linear-gradient(to bottom, #F7F7F7, #ffffff);

				.groupinfo_top {
					display: flex;
					align-items: center;

					.group_img_box {
						width: 145rpx;
						height: 145rpx;
						position: relative;

						.group_img {
							width: 145rpx;
							height: 145rpx;
							border-radius: 8rpx;
							vertical-align: middle;
						}

						.group_border_img {
							position: absolute;
							top: 0;
							right: 0;
							bottom: 0;
							left: 0;
							width: 145rpx;
							height: 145rpx;
							z-index: 1;
							vertical-align: middle;
						}
					}

					.group_title_box {
						flex: 1;
						height: 150rpx;
						margin-left: 15rpx;
						position: relative;

						.group_qrcode_mini {
							width: 40rpx;
							height: 40rpx;
						}

						.group_addr_box {
							position: relative;
							color: #00A0DC;
							font-size: 18rpx;
							display: inline-flex;
							padding: 2rpx 10rpx;
							margin-top: 10rpx;
							border-radius: 8rpx;
							border: 1rpx solid #00A0DC;
						}

						.group_user_box {
							font-size: 24rpx;
							position: absolute;
							bottom: 2rpx;
							left: 0;
							right: 0;
							display: flex;
							align-items: center;
							justify-content: space-between;
						}
					}
				}

				.groupinfo_text {
					color: #999999;
					font-size: 24rpx;
					margin-top: 50rpx;
					line-height: 32rpx;
				}

				.mygroups_avatar_box {
					width: 120rpx;
					height: 120rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;
					border-radius: 100%;

					.groups_user_img {
						width: 95rpx;
						height: 95rpx;
						border-radius: 100px;
					}

					.mygroups_avatar_border {
						width: 120rpx;
						height: 120rpx;
						position: absolute;
						top: 0;
						right: 0;
						bottom: 0;
						left: 0;
						z-index: 1;
					}
				}

				.grouplist_name_box {
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}


				.uni-col-5 {
					width: 20% !important;
				}
			}
		}
	}
</style>